<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Controller
 * @subpackage Action
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Error.php 7 2009-11-14 22:42:08Z nkeyes $
 */

require_once 'Zend/Controller/Action.php'; 


/**
 * 
 * @author Nathan Keyes
 *
 */
class Bushido_Controller_Action_Error extends Bushido_Controller_Action
{
	protected $_errorHandlerExceptions = array();
	
	
	/**
	 * (non-PHPdoc)
	 * @see library/Zend/Controller/Zend_Controller_Action#init()
	 */
	public function init()
	{
		parent::init();
		$this->_requiresAuth = false;

	}
	
	
	/**
	 * 
	 * @return void
	 */
	public function errorAction()
	{
		//echo "error!";exit;
		$errors = $this->_getParam('error_handler');
		$exception = $errors->exception;
		$this->view->exception = $exception;
		$appConfig = Zend_Controller_Front::getInstance()->getParam('appconfig');
		
		$this->view->showErrorDetails = $appConfig['showerrordetails'];
		$this->view->defaultErrorMessage = $appConfig['defaulterrormessage'];
		//Zend_Debug::dump($exception);exit;
		$this->_logError($exception);
		switch($errors->type)
		{
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
				$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
				$this->getResponse()->setHttpResponseCode(404);
				$this->view->errorMessage = "404: Page Not Found";
				break;
			default:
				switch(get_class($exception))
				{
					/*
					case 'Bushido_Db_Table_Exception_RowNotFound':
						$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
						$this->view->errorMessage = "404: Page Not Found";
						$this->getResponse()->setHttpResponseCode(404);
						break 2;
					*/
					case 'Bushido_Acl_InsufficientAccessException':
					case 'Zend_Acl_Exception':
						$this->getResponse()->setRawHeader('HTTP/1.1 403 Forbidden');
						$this->view->errorMessage = "403: Forbidden";
						$this->getResponse()->setHttpResponseCode(403);
						break 2;
				}
				//$this->view->errorMessage = print_r($exception, true);
				$this->view->errorMessage = join("<br />\n", array($exception->getMessage(), $exception->getFile(), get_class($exception) . " on line ". $exception->getLine() )) . "<br />\n";
				if(!empty($this->_errorHandlerExceptions))
				{
					foreach($this->_errorHandlerExceptions as $errorHandlerException)
					{
						$this->view->errorMessage .= join("<br />\n", array($errorHandlerException->getMessage(), $errorHandlerException->getFile(). " on line ". $errorHandlerException->getLine() )) . "<br />\n";
					}
					echo $this->view->errorMessage;
					unset($errorHandlerException);
				}
				break;
		}
	}
	
	protected function _logError($exception)
	{
		
		try
		{
			$frontController = Zend_Controller_Front::getInstance();
			$request = $frontController->getRequest();
			$response = $frontController->getResponse();
			$adapter = Bushido_Db_Table::getAdapter();
			
			
			$errorLog = array
			(
				'ip_address' => $_SERVER['REMOTE_ADDR'],
				'user_agent' => $_SERVER['HTTP_USER_AGENT'],
				'uri' => $request->getRequestUri(),
				'params' => print_r($request->getParams(), true),
				'error_class' => get_class($exception),
				'stack_trace' => $exception->getTraceAsString(),
				'file' => $exception->getFile(),
				'line' => $exception->getLine(),
				'error_message' => $exception->getMessage(),
				'timestamp' => new Zend_Db_Expr('NOW()'),
			);
			
			$user = Bushido_App::getUser();
			//Zend_Debug::dump($user, 'user:');
			if($user && $user->toArray())
			{
				$errorLog['bushido_user_id'] = $user->bushido_user_id;
			}
			$adapter->insert
			(
				'error_log',
				$errorLog
			);
		}
		catch(Exception $e)
		{
			$this->_errorHandlerExceptions[] = $e;
		}
		
	}
}